// home的控制器
angular.module('goods.controller', ['goods.service'])
  .controller("GoodsCtrl", function ($scope,$window) {
    getHeaderSlideData();
    headerChangeColor()
    goTop();
    // 监听页面激活事件,产生swiper的bug的原因就是因为时间差问题，初始化swiper的时候页面还没有渲染完毕
    $scope.$on('$ionicView.enter',function(){
      initHeaderSlide();

    })
    // 头部滚动条数据
    function getHeaderSlideData(){
      $scope.headerSlideData=[
        {
          alt:"双十一预热主场会",
          src:"img/goods/goods-01.jpg"
        },
        {
          alt:"11月11天家电低价不停歇",
          src:"img/goods/goods-02.jpg"
        },
        {
          alt:"家具盛典 好货提前抢",
          src:"img/goods/goods-03.jpg"
        },
        {
          alt:"IT抢券节",
          src:"img/goods/goods-04.jpg"
        },
        {
          alt:"潮流数码 双11爽购攻略",
          src:"img/goods/goods-05.jpg"
        }
      ];
    }

    // 初始化头部滚动条
    function initHeaderSlide(){
      var headerSwiper = new Swiper('#headerSlider', {
        slidesPerView: 1,
        paginationClickable: true,
        centeredSlides: true,
        autoplay: 2000,
        autoplayDisableOnInteraction: false,
        loop: true,
        // 如果需要分页器
        pagination: '.swiper-pagination',
        // 改变自动更新
        observer:true,
        observeParents:true


        //paginationClickable: true,
        //autoplay: 2000,
        //autoplayDisableOnInteraction: false,
        //loop: true,
        //// 如果需要分页器
        //pagination: '.swiper-pagination',
        //// 改变自动更新
        //observer:true,
        //observeParents:true
      });
    }

    // 改变头部颜色
    function headerChangeColor(){
      var bg=$window.document.getElementById('home-content');
      console.log(bg);
      var nowOpacity=0;
      bg.onscroll=function(event){
        if(this.scrollTop/250<.85){
          nowOpacity=this.scrollTop/250;
        }
        document.getElementById("headerBar-bg").style.opacity=nowOpacity;
      }
    }
//回到顶部
    function goTop(){
      var bg=window.document.getElementById('home-content');
      var goTop = document.querySelector(".back_top");

      bg.addEventListener('scroll',function(){
        var top = bg.scrollTop;
        if(top>200){
          goTop.style.opacity = 1;
        }else{
          goTop.style.opacity = 0;
        }
      },false);

      goTop.onclick = function(){
        bg.scrollTop = 0;
      }
    };


  })
